package com.learn.ThreadPoolLearnTwo;

import org.apache.log4j.Logger;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/25
 */
public class NewSingleThreadExecutorTest {
    private static final Logger log=Logger.getLogger(NewSingleThreadExecutorTest.class);

    public static void main(String[] args) throws Exception {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);

        List<Future<String>> futures = fixedThreadPool.invokeAll(Arrays.asList(
                () -> {
                    log.debug("begin");
                    Thread.sleep(1000);
                    return "1";
                },
                () -> {
                    log.debug("begin");
                    Thread.sleep(1000);
                    return "2";
                },
                () -> {
                    log.debug("begin");
                    Thread.sleep(1000);
                    return "3";
                }
        ));

        futures.forEach(f-> {
            try {
                log.debug(f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });

        fixedThreadPool.shutdownNow();


//
//        Future<String> future2= fixedThreadPool.submit(()->{
//            log.debug("线程开始执行");
//            Thread.sleep(1000);
//            return "ok";
//        });


        /*Future<String> future = fixedThreadPool.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                log.debug("线程开始执行");
                Thread.sleep(1000);
                return "ok";
            }
        });
        log.debug("future:"+future.get());*/

    }
}
